package com.smart.campus.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.ShStudentMapper;
import com.smart.campus.dto.ShStudent;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.ShStudentService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class ShStudentServiceImpl implements ShStudentService {
    @Autowired
    private ShStudentMapper shStudentMapper;

    @Override
    public PagedResponse<ShStudent> pageShStudent(ShStudent shStudent, Integer pageNum, Integer pageSize) {
        Page<ShStudent> page = new Page<>(pageNum, pageSize);
        Page<ShStudent> page1 = shStudentMapper.pageShStudent(shStudent,page);
        return PaginationUtil.getPaginationResponse(page1.getRecords(),pageNum,pageSize,page1.getTotal());
    }

    @Override
    public void addShStudent(ShStudent shStudent) {
        shStudentMapper.insert(shStudent);
    }

    @Override
    public void delShStudent(Integer id) {
        shStudentMapper.deleteById(id);
    }

    @Override
    public ShStudent findShStudentById(Integer id) {
        return shStudentMapper.selectById(id);
    }

    @Override
    public void updateDateShStudent(ShStudent shStudent) {
        shStudentMapper.updateById(shStudent);
    }

    /**
     * 导出
     * @param shStudent
     * @return
     */
    @Override
    public List<ShStudent> findShStudentList(ShStudent shStudent) {
        return shStudentMapper.findShStudentList(shStudent);
    }

    /**
     * 导入
     * @param file
     */
    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        //获得excel中的数据
        List<ShStudent> list = ExcelImportUtil.importExcel(file.getInputStream(), ShStudent.class, params);
        list.forEach(shStudent -> {
            shStudentMapper.insert(shStudent);
        });
    }
}
